/*
 * xAPdialog.java
 *
 * xAP properties dialog
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 13/10/09 by Daniel Berenguer : first version
 */

package xAP;

import javax.swing.DefaultListModel;
import xAP.xAPtypes.TypeOfElement;


public class xAPdialog extends javax.swing.JDialog
{
    /** Creates new form xAPdialog */
    public xAPdialog(TypeOfElement type)
    {
        elemType = type;    // Type of element

        initComponents();

        // Model for the list of devices
        listModelDevices = new DefaultListModel();
        listDevices.setModel(listModelDevices);
        // Model for the list of endpoints
        listModelEndpoints = new DefaultListModel();
        listEndpoints.setModel(listModelEndpoints);

        fillDevices();

        this.setTitle("xAP source address selector");

        // Center dialog
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listDevices = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        listEndpoints = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        textXapUID = new javax.swing.JTextField();
        textXapSource = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setBounds(new java.awt.Rectangle(0, 0, 580, 520));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setText("xAP BSC devices");

        listDevices.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listDevices.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listDevicesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listDevices);

        listEndpoints.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listEndpoints.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listEndpointsValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(listEndpoints);

        jLabel2.setText("BSC Endpoints");

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        jButton2.setText("OK");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKbuttonActionPerformed(evt);
            }
        });

        jLabel3.setText("xAP UID");

        jLabel4.setText("xAP source address");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(9, 9, 9))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textXapSource, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(textXapUID, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textXapSource, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textXapUID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(22, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
   /********************************************************************************************
    * listDevicesValueChanged
    *
    * Function called whenever a device is selected from the list
    *
    ********************************************************************************************/
    private void listDevicesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listDevicesValueChanged
        // Check device selection
        int index = listDevices.getSelectedIndex();
        // Fill list of endpoints
        fillEndpoints(index);
    }//GEN-LAST:event_listDevicesValueChanged

   /********************************************************************************************
    * OKbuttonActionPerformed
    *
    * Function called whenever the OK button is pressed
    *
    ********************************************************************************************/
    private void OKbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKbuttonActionPerformed
        boolResult = true;
        this.dispose();
    }//GEN-LAST:event_OKbuttonActionPerformed

   /********************************************************************************************
    * listEndpointsValueChanged
    *
    * Function called whenever an endpoint is selected from the list
    *
    ********************************************************************************************/
    private void listEndpointsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listEndpointsValueChanged
        // Fill xAP source text field
        String strDevice = (String) listDevices.getSelectedValue();
        String strEndpoint = (String) listEndpoints.getSelectedValue();
        textXapSource.setText(strDevice + ":" + strEndpoint);

        // Fill xAP UID text field
        int index = listEndpoints.getSelectedIndex();
        String strUID = drvCore.bscEndpoints.endpointAt(index).uid;
        textXapUID.setText(strUID);
    }//GEN-LAST:event_listEndpointsValueChanged

   /********************************************************************************************
    * CancelButtonActionPerformed
    *
    * Function called whenever the Cancel button is pressed
    *
    ********************************************************************************************/
    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        boolResult = false;
        this.dispose();
}//GEN-LAST:event_CancelButtonActionPerformed

    private boolean boolResult = false;
    TypeOfElement elemType;

    private DefaultListModel listModelDevices;
    private DefaultListModel listModelEndpoints;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listDevices;
    private javax.swing.JList listEndpoints;
    private javax.swing.JTextField textXapSource;
    private javax.swing.JTextField textXapUID;
    // End of variables declaration//GEN-END:variables

   /********************************************************************************************
    * fillDevices
    *
    * Fill list of devices
    *
    ********************************************************************************************/
    private void fillDevices()
    {
        int i;

        for(i=0 ; i<drvCore.xAPdevices.length() ; i++)
        {
            listModelDevices.addElement(drvCore.xAPdevices.deviceAt(i).source);
        }
    }

   /********************************************************************************************
    * fillEndpoints
    *
    * Fill list of endpoints
    *
    * 'intDev'  index selected in the list of devices
    * 
    ********************************************************************************************/
    private void fillEndpoints(int intDev)
    {        
        int i;
        xAPdevice device = drvCore.xAPdevices.deviceAt(intDev);
        xAPBSCendpoint bscEndpoint;

        listModelEndpoints.clear();

        for(i=0 ; i<drvCore.bscEndpoints.length() ; i++)
        {
            bscEndpoint = drvCore.bscEndpoints.endpointAt(i);

            if (bscEndpoint.belongsToDevice(device))
            {
                if (elemType == TypeOfElement.BSC_INPUT_NUM || elemType == TypeOfElement.BSC_INPUT_STA ||
                    bscEndpoint.isOutput())
                {
                    // Remove "device:" string from the xAP source address
                    int start = device.source.length() + 1;
                    int end = drvCore.bscEndpoints.endpointAt(i).source.length();

                    String strEndpoint = drvCore.bscEndpoints.endpointAt(i).source.substring(start, end);
                    listModelEndpoints.addElement(strEndpoint);
                }
            }
        }
    }

   /********************************************************************************************
    * getSourceAddr
    *
    * Return selected xAP source address to the parent element
    *
    * Returns:
    *   xAP source address
    ********************************************************************************************/
    public String getSourceAddr()
    {
        return textXapSource.getText();
    }

   /********************************************************************************************
    * getResult
    *
    * Return true or false depending on the button pressed
    *
    * Returns:
    *   true if OK was pressed
    *   false otherwise
    ********************************************************************************************/
    public boolean getResult()
    {
        return boolResult;
    }
}
